package com.ltxtiyu.video.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.ltxtiyu.video.entity.Config;
import com.ltxtiyu.video.service.ms.ConfigrationService;
import com.ltxtiyu.video.tool.ObjectTool;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@RequestMapping("configrationController")
@Controller
public class ConfigrationController {

    @Reference
    private ConfigrationService cs;

    /**
     * 根据key查看数据库的缓存库的config
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/detailConfig")
    public String detailConfig(HttpServletRequest request){
        String ckey = request.getParameter("ckey");
        if(StringUtils.isNotBlank(ckey)){
            Config config = cs.detailConfig(ckey);
            return JSON.toJSONString(config);
        }
        return "fail";
    }


    /**
     * 根据key删除数据库的缓存库的config
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteConfig")
    public String deleteConfig(HttpServletRequest request){
        String ckey = request.getParameter("ckey");
        if(StringUtils.isNotBlank(ckey)){
            cs.deleteConfig(ckey);
            return "success";
        }
        return "fail";
    }

    /**
     * 根据key修改数据库的缓存库的config
     * 注意参数必须完整
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateConfig")
    public String updateConfig(HttpServletRequest request){
        Integer id = ObjectTool.numberOrNull(request.getParameter("id"));
        String ckey = ObjectTool.stringOrNull(request.getParameter("ckey"));
        String cvalue = ObjectTool.stringOrNull(request.getParameter("cvalue"));
        String description = ObjectTool.stringOrNull(request.getParameter("description"));
        Integer status = ObjectTool.numberOrNull(request.getParameter("status"));
        if(id == null || ckey == null || cvalue == null || description == null || status == null){
            return "参数不足";
        }
        Config config = new Config();
        config.setId(id);
        config.setCkey(ckey);
        config.setCvalue(cvalue);
        config.setDescription(description);
        config.setStatus(status);
        cs.updateConfig(config);
        return "success";
    }
}
